package agh.tk.markuptranslation;

public class Table extends AbstractComponent
{	
	private static boolean isTableBegining = true; 
	/**
	 * 
	 * @param leftChild - Wiersz
	 * @param rightChild - Reszta tabeli
	 */
	public Table(IComponent leftChild, IComponent rightChild) 
	{
		super(leftChild, rightChild);
	}
	@Override
	public String toDokuWiki()
	{
		StringBuilder stringBuilder = new StringBuilder() ;
		if(leftChild instanceof TableHeader)
			stringBuilder.append("^") ;
		else 
			stringBuilder.append("|") ;
		if(leftChild instanceof TableHeader)
			stringBuilder.append(super.toDokuWiki("^\n")) ;
		else
			stringBuilder.append(super.toDokuWiki("|\n")) ;
		if(rightChild == null)
			if(leftChild instanceof TableHeader)
				stringBuilder.append("^") ;
			else 
				stringBuilder.append("|") ;
		return stringBuilder.toString() ;
	}
	
	@Override
	public String toMediaWiki() {
		return null ;
	}
	
	@Override
	public String toHTML() {
		if(leftChild == null)
			return "";
		boolean isEnding = false;
		StringBuilder result = new StringBuilder();
		if(isTableBegining) {
			isTableBegining = false;
			result.append("<table>\n");
			isEnding = true;
		}
		result.append("<tr>\n");
		result.append(leftChild.toHTML());
		result.append("</tr>\n");
		if(rightChild!=null) {
			result.append(rightChild.toHTML());
		}
		if(isEnding) {
			isTableBegining = true;
			result.append("</table>");
		}
		return result.toString();
	}
	
	@Override
	public String toTxt2tags()
	{
		StringBuilder stringBuilder = new StringBuilder() ;
		if(leftChild instanceof TableHeader)
			stringBuilder.append("||") ;
		if(leftChild instanceof TableRow)
			stringBuilder.append("|") ;
		stringBuilder.append(leftChild.toTxt2tags()) ;
		stringBuilder.append("|\n") ;
		if(rightChild != null)
		{
			//stringBuilder.append("|") ;
			stringBuilder.append(rightChild.toTxt2tags()) ;
			stringBuilder.append("\n") ;
		}
		return stringBuilder.toString() ;
	}
}
